using Newtonsoft.Json;
using LiJ.Common.Core;
using LiJ.Http.Mos;
using LiJ.Framework.Official.Basic.Mos;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace LiJ.Framework.Official.Basic
{
	/// <summary>
	/// 公号管理
	/// </summary>
	public class WxOffMenuApi : WxOffBaseApi
	{
		/// <summary>
		/// 构造函数
		/// </summary>
		/// <param name="config">配置信息，如果这里不传，需要在程序入口静态 WxBaseApi.DefaultConfig 属性赋值</param>
		public WxOffMenuApi(AppConfig config = null)
			: base(config)
		{
		}

		/// <summary>
		///    添加或更新公号菜单
		/// </summary>
		/// <param name="buttons"></param>
		/// <returns></returns>
		public async Task<WxBaseResp> AddOrUpdateMenuAsync(List<WxMenuButtonMo> buttons)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/menu/create";
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(new
			{
				button = buttons
			}, Formatting.Indented, new JsonSerializerSettings
			{
				NullValueHandling = NullValueHandling.Ignore
			});
			return await base.RestCommonOffcialAsync<WxBaseResp>(LHttpRequest, null);
		}

		/// <summary>
		/// 获取菜单设置
		/// </summary>
		/// <returns></returns>
		public async Task<WxGetMenuResp> GetMenuAsync()
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Get;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/menu/get";
			return await base.RestCommonOffcialAsync<WxGetMenuResp>(LHttpRequest, null);
		}

		/// <summary>
		/// 删除菜单
		/// </summary>
		/// <returns></returns>
		public async Task<WxBaseResp> DeleteMenuAsync()
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Get;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/menu/delete";
			return await base.RestCommonOffcialAsync<WxBaseResp>(LHttpRequest, null);
		}

		/// <summary>
		///   添加定制个性化菜单
		/// </summary>
		/// <param name="buttons"></param>
		/// <param name="rule"></param>
		/// <returns></returns>
		public async Task<WxAddCustomMenuResp> AddCustomMenuAsync(List<WxMenuButtonMo> buttons, WxMenuMatchRuleMo rule)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/menu/addconditional";
			LHttpRequest.CustomBody = JsonConvert.SerializeObject(new
			{
				button = buttons,
				matchrule = rule
			}, Formatting.Indented, new JsonSerializerSettings
			{
				NullValueHandling = NullValueHandling.Ignore
			});
			return await base.RestCommonOffcialAsync<WxAddCustomMenuResp>(LHttpRequest, null);
		}

		/// <summary>
		/// 删除个性化菜单
		/// </summary>
		/// <param name="menuid">菜单Id</param>
		/// <returns></returns>
		public async Task<WxBaseResp> DeleteCustomMenuAsync(long menuid)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/menu/delconditional";
			LHttpRequest.CustomBody = string.Format("{{\"menuid\":\"{0}\"}}", menuid);
			return await base.RestCommonOffcialAsync<WxBaseResp>(LHttpRequest, null);
		}

		/// <summary>
		/// 获取某个用户下的菜单信息
		/// </summary>
		/// <param name="userId">用户openid，或者微信号</param>
		/// <returns></returns>
		public async Task<WxUserMenuResp> GetUserMenuAsync(string userId)
		{
			LHttpRequest LHttpRequest = new LHttpRequest();
			LHttpRequest.HttpMothed = HttpMothed.Post;
			LHttpRequest.AddressUrl = "https://api.weixin.qq.com" + "/cgi-bin/menu/trymatch";
			LHttpRequest.CustomBody = string.Format("{{\"user_id\":\"{0}\"}}", userId);
			return await base.RestCommonOffcialAsync<WxUserMenuResp>(LHttpRequest, null);
		}
	}
}
